package com.ricchieri.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.ricchieri.dao.interf.ICharterDao;
import com.ricchieri.filter.CharterFilter;
import com.ricchieri.model.Charter;
import com.ricchieri.service.interf.ICharterService;
import com.ricchieri.util.FacesUtil;

@Transactional(readOnly = true)
public class CharterService implements ICharterService, Serializable {
	private static final long serialVersionUID = -2141276597707083953L;
	ICharterDao charterDao;
	
	@Override
	@Transactional(readOnly = true)
	public List<Charter> getAllCharter() {
		CharterFilter filter = new CharterFilter();
		return charterDao.getAllCharter(filter);
	}

	public ICharterDao getCharterDao() {
		return charterDao;
	}

	public void setCharterDao(ICharterDao charterDao) {
		this.charterDao = charterDao;
	}
	
	@Transactional(readOnly = false)
	public void addCharter(Charter charter){
		charterDao.add(charter);
	}
	
	@Transactional(readOnly = false)
	public void deleteCharter(Charter charter) {
		charter.setActive(Boolean.FALSE);
		charterDao.update(charter);
	}

	@Override
	public List<Charter> getAllCharterPaginatedByFilter(
			CharterFilter charterFilter, int startRecord) {
		return charterDao.getAllCharterPaginatedByFilter(charterFilter, startRecord);
	}

	@Override
	public int getAllCharterCountByFilter(CharterFilter charterFilter) {
		return charterDao.getAllCharterCountByFilter(charterFilter);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateCharter(Charter charter) {
		charterDao.update(charter);
	}
	
	@Override
	public Charter getCharterById(CharterFilter charterFilter) {
		return charterDao.getCharterById(charterFilter);
	}	
	
	@Override
	public Map<String, String> getComboCharters() {
		List<Charter> charterT = this.getAllCharter();
		HashMap<String, String> charters = new HashMap<String, String>();
		String description;

		for (Charter charter : charterT) {
			description = FacesUtil.getMessage("scheduleCharterNumber")
					.concat(" ")
					.concat(String.valueOf(charter.getCharterNumber()));
			description = description.concat(" - ")
					.concat(FacesUtil.getMessage("scheduleCharterPlaces"))
					.concat(" ")
					.concat(String.valueOf(charter.getPlaces()));
			charters.put(description, String.valueOf(charter.getId()));
		}

		return charters;
	}	
}
